package com.zhanghe.study.spring4.annotation.config.registrar;

import com.zhanghe.study.spring4.annotation.config.bean.Red;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.context.annotation.ImportBeanDefinitionRegistrar;
import org.springframework.core.type.AnnotationMetadata;

/**
 * 自定义的组件注册器
 * @author zh
 * @date 2021/2/21 12:51
 */
public class MyImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
    @Override
    public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
        boolean color = registry.containsBeanDefinition("com.zhanghe.study.spring4.annotation.config.bean.Color");
        if(color){
            BeanDefinition beanDefinition = new RootBeanDefinition(Red.class);
            registry.registerBeanDefinition("red", beanDefinition);
        }
    }
}
